#include<stdio.h>
#include<unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <lcd.h>
#include <stdlib.h>
#include "touch.h"
#include "jpg.h"
#include "mp3.h"
#include <pthread.h>
#include "filelist.h"
#include "game.h"
#include "music.h"
char path[256]="/home/xc/wuzi/jpg",chess_path[256];
int exit_flag=0;
int black_score=0,white_score=0,music;
extern Head_List * jpg_list;
extern Head_List * mp3_list;
int main() //开始游戏
{
	LCD_Init();
	printf("\njpg_list\n");
	Search_Pics(path);
	Print_List_Msg(jpg_list);
	Print_List_Msg(mp3_list);
	pthread_t  tid1;
	pthread_create(&tid1, NULL,music_run, NULL);
	while(1)
	{
		int XP;
		int YP;
		int XP_PRE,YP_PRE,begin=0,jpg_flag=0,direction=0,main_flag=0;
		char pathname[512];
		// JPEG_Display("/home/xc/12.jpg", 510,80);
		// pthread_t mid;
		Game_Begin();
		fNode *jpg_node=NULL;
		jpg_node=jpg_list->first;
		sprintf(pathname, "%s/%s", path, jpg_node->filename);
		int mark_who=1;//谁的回合，1——黑 2——白
		int win_who=0;//谁赢 1-黑胜 2-白胜
		//int wu_point[15][15];//构建棋盘点，0-无		  1-黑	2-白
		Game_init(&win_who,pathname);
		while(1)
		{	
			XP_PRE=XP,YP=YP_PRE;
			Get_Point(&XP,&YP);
			direction = Get_Move_Direction();
			jpg_flag=Photo_Change(XP,YP,direction);
			if(jpg_flag==1)
			{
				jpg_node=jpg_node->next;
				jpg_flag=0;
			}
			if(jpg_flag==-1)
			{
				jpg_node=jpg_node->prev;
				jpg_flag=0;
			}
			sprintf(pathname, "%s/%s", path, jpg_node->filename);
			JPEG_Display(pathname, 510, 30);
			if(music == 0 && (XP!=XP_PRE || YP!=YP_PRE) )			
				system("madplay -Q 10.mp3 &");

			Go_point(&XP,&YP,&mark_who);
			win_who= panduan(XP,YP);//接受判断结果
			
			if(win_who==1)
			{
				printf("BLACK  WIN!!!\n");
				JPEG_Display("/home/xc/wuzi/game/11.jpg", 0, 0);
				begin=1;
				black_score++;
			}
			if(win_who==2)
			{
				printf("WHITE  WIN!!!\n");
				JPEG_Display("/home/xc/wuzi/game/10.jpg",0, 0);
				begin=1;
				white_score++;
			}
			while(begin)
			{
				int flag=0;
				XP_PRE=XP;YP_PRE=YP;
				Get_Point(&XP,&YP);
				if(XP_PRE !=XP |YP !=YP_PRE)
					flag=1;
				while(flag)
				{
					Game_Last();
					Get_Point(&XP,&YP);
					if(XP>=300 && XP<=500 &&YP>=100 &&YP<=150)
					{
						Game_resee(pathname);
						flag=0;
					}
					if(XP>=300 && XP<=500 &&YP>=170 &&YP<=220)
					{
						Game_init(&win_who,pathname);
						flag=0;
						begin=0;
					}
					if(XP>=300 && XP<=500 &&YP>=240 &&YP<=290)
					{
						flag=0;
						begin=0;
						main_flag=1;
					}
					if(XP>=300 && XP<=500 &&YP>=430 &&YP<=480)
					{
						flag=0;
						begin=0;
						main_flag=1;
						exit_flag=1;
						JPEG_Display("/home/xc/wuzi/game/28.jpg", 0, 0);
					}
				}
				
			}
			if(main_flag)
				break;
		}
		if(exit_flag)
			break;
	}
	destroy_list(jpg_list);
	Print_List_Msg(jpg_list);
	LCD_Uninit();
	exit(0);
	return 1;
}

